// 导入路由模块下的静态路由表
import { constantRoutes, asyncRoutes } from '@/router/index'
const state = () => {
  return {
    // 路由表：初始值是静态路由表的值
    routes: constantRoutes
  }
}

const mutations = {
  setRoutes: (state, showAsyncRoutes) => {
    // 给routes数组追加筛选后的动态路由表
    state.routes.push(...showAsyncRoutes)
  },
  removeRoutes: (state) => {
    state.routes = []
  }
}

const actions = {
  // 筛选动态路由表
  asyncRoutesAction: (store, menus) => {
    // 基于动态路由表和menus数组筛选
    const showAsyncRoutes = asyncRoutes.filter(item => menus.includes(item.children[0].name))
    // 筛选完毕后提交mutations，传入筛选结果作为参数
    store.commit('setRoutes', showAsyncRoutes)
    // 返回筛选结果(给路由守卫用)
    return showAsyncRoutes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
